texthistory: fix calculation of n_chars
authorChristian Hergert <chergert@redhat.com>
Fri, 16 Jul 2021 02:19:57 +0000 (19:19 -0700)
committerChristian Hergert <chergert@redhat.com>
Fri, 16 Jul 2021 04:26:30 +0000 (21:26 -0700)
This should be the number of characters, not the end position. This fixes
an issue where we wouldn't coalesce insert actions together.

gtk/gtktexthistory.c
testsuite/gtk/texthistory.c

index 1d97ff31a76be2e232699fbf33ef7ac555fcbd79..61ccc33f08511430fa76243535072e803d8abb9b 100644 (file)
@@ -989,6 +989,7 @@ gtk_text_history_text_inserted (GtkTextHistory *self,
                                 int             len)
 {
   Action *action;
+  guint n_chars;
 
   g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
 
@@ -998,14 +999,12 @@ gtk_text_history_text_inserted (GtkTextHistory *self,
 
   if (len < 0)
     len = strlen (text);
+  n_chars = g_utf8_strlen (text, len);
 
   action = action_new (ACTION_KIND_INSERT);
   action->u.insert.begin = position;
-  action->u.insert.end = position + g_utf8_strlen (text, len);
-  istring_set (&action->u.insert.istr,
-               text,
-               len,
-               action->u.insert.end);
+  action->u.insert.end = position + n_chars;
+  istring_set (&action->u.insert.istr, text, len, n_chars);
 
   gtk_text_history_push (self, action);
 }
index f9fe45ceb26d4221cd0030dcafb22540233145ed..c64c803083d0c8e674da62002c5c22ea8e94eac1 100644 (file)
@@ -578,6 +578,32 @@ test13 (void)
   run_test (commands, G_N_ELEMENTS (commands), 3);
 }
 
+static void
+test14 (void)
+{
+  char *fill = g_strnfill (1024, 'x');
+  char *fill_after = g_strnfill (1025, 'x');
+  char *fill_after_2 = g_strdup_printf ("%s word", fill_after);
+  const Command commands[] = {
+    { BEGIN_USER, -1, -1, NULL, NULL, UNSET, UNSET, UNSET },
+    { INSERT, 0, -1, fill, fill, UNSET, UNSET, UNSET },
+    { END_USER, -1, -1, NULL, NULL, SET, UNSET, UNSET },
+    { BEGIN_USER, -1, -1, NULL, NULL, UNSET, UNSET, UNSET },
+    { INSERT, 0, -1, "x", fill_after, UNSET, UNSET, UNSET },
+    { END_USER, -1, -1, NULL, NULL, SET, UNSET, UNSET },
+    { BEGIN_USER, -1, -1, NULL, NULL, UNSET, UNSET, UNSET },
+    { INSERT_SEQ, strlen(fill_after), -1, " word", fill_after_2, UNSET, UNSET, UNSET },
+    { END_USER, -1, -1, NULL, NULL, SET, UNSET, UNSET },
+    { UNDO, -1, -1, NULL, fill_after, SET, SET, UNSET },
+    { UNDO, -1, -1, NULL, fill, SET, SET, UNSET },
+    { UNDO, -1, -1, NULL, "", UNSET, SET, UNSET },
+  };
+
+  run_test (commands, G_N_ELEMENTS (commands), 0);
+
+  g_free (fill);
+}
+
 int
 main (int   argc,
       char *argv[])
@@ -597,6 +623,7 @@ main (int   argc,
   g_test_add_func ("/Gtk/TextHistory/test11", test11);
   g_test_add_func ("/Gtk/TextHistory/test12", test12);
   g_test_add_func ("/Gtk/TextHistory/test13", test13);
+  g_test_add_func ("/Gtk/TextHistory/test14", test14);
 
   return g_test_run ();
 }